/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.apt.ServletClient;

import com.myapp.DAO.MemberDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apt.Bean.PaymentSeminarEntities;
import com.myapp.DAO.PaymentSeminarDao;
import com.myapp.DAO.SeminarDao;
import org.apt.Bean.MemberEnties;
import org.apt.connectDB.Mail;

/**
 *
 * @author Administrator
 */
public class registerSeminar extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession(true);
            String username = session.getAttribute("UserID").toString();
            MemberEnties member = MemberDao.getMemberByUserName(username);
            String seminarID = request.getParameter("seminarID");
            String groupPyamentID = request.getParameter("ddlgroupPayment");

            PaymentSeminarEntities seminar = PaymentSeminarDao.getPaymentSeminarByUserAndID(username, Integer.parseInt(seminarID));
            if (seminar.getUser() != null) {
                if (seminar.isStatusPayment() == false) {
                    out.print("<script>alert('This Seminar had been registed you must wait until admin approve!');window.location.href='Seminar.jsp?pageno=1';</script>");
                } else {
                    out.print("<script>alert('Sorry,This Seminar had been registed you can not register more one please choice a diffirent event!');window.location.href='Seminar.jsp?pageno=1';</script>");
                }
            } else {
                if (SeminarDao.CheckCurrentJoinRegisterSeminar(Integer.parseInt(seminarID))) {
                    PaymentSeminarEntities seminarPayment = new PaymentSeminarEntities();
                    seminarPayment.setUser(username);
                    seminarPayment.setSeminarID(Integer.parseInt(seminarID));
                    seminarPayment.setGroupPaymentID(Integer.parseInt(groupPyamentID));
                    seminarPayment.setStatusPayment(false);
                    seminarPayment.setStatus(false);
                    seminarPayment.setDescription("");
                    boolean resuilt = PaymentSeminarDao.InsertPaymentSeminar(seminarPayment);
                    if (resuilt) {
                        String subject = "National level paper!";
                        String body = "Hello! " + username + " \nThank you for registration seminar!\n We will review and respond to your messages in the shortest time payment when you finish!\n\nNational Level Paper Presentation-Group 183";
                        Mail.sendMail(member.getTxtEmail(), subject, body);
                        out.print("<script>alert('Register seminar success please wait We will send message to you after addmin approve, Thank you!');window.location.href='index.jsp';</script>");
                    } else {
                        out.print("<script>alert('Register seminar fails, please try again!');window.location.href='index.jsp';</script>");
                    }
                } else {
                    out.print("<script>alert('Sorry, Seminar had ben enough member or Event day pass time event please choice a diffirent event!');window.location.href='Seminar.jsp?pageno=1';</script>");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
